<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------


namespace app\common\model;


use think\Model;
use think\model\concern\SoftDelete;

/**
 * 有关时间的模型
 * Class TimeModel
 * @package app\common\model
 */
class TimeModel extends Model
{

    /**
     * 自动时间戳类型
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 添加时间
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 软删除
     */
    use SoftDelete;

    protected $deleteTime = false;

    public function getStatusList()
    {
        return ['0' => '禁用', '1' => '启用',];
    }

    /**
     * @param int $id
     * @param array $data
     * @return TimeModel
     */
    public function updateDataSave(int $id = 0, array $data = [])
    {
        return $this->where('id', $id)
            ->update($data, [
                "id" => $id
            ]);
    }

    /**
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIndexListInfo(array $where = [], string $field = '*'): array
    {
        return $this->field($field)->where($where)
            ->where('status', 1)
            ->select()
            ->toArray();
    }

    /**
     * @param array $where
     * @param string $field
     * @param int $limit 每页数量
     * @param int $page 当前页
     * @param bool $simple 是否为简洁分页
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getIndexListPage(array $where = [], string $field = '*', int $limit = 10, string $orderBy = 'id desc', bool $simple = false): array
    {
        return $this->field($field)->where($where)
            ->where('status', 1)
            ->order($orderBy)
            ->paginate($limit, $simple)
            ->toArray();
    }

}